//
//  JXUserAccount.swift
//  01-weiboSwift
//
//  Created by TwistFate on 16/2/28.
//  Copyright © 2016年 TwistFate. All rights reserved.
//

import UIKit

class JXUserAccount: NSObject ,NSCoding {
    
    var access_token : String?
    
    var expires_in : NSTimeInterval = 0{
        didSet {
            // 当别人给expires_in属性赋值时,我们将这个expires_in转成expiresDate,方便比较时间
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }

    
    // 过期时间,用于比较是否过期
    var expiresDate: NSDate?
    
    var uid : String?
    
    var screen_name : String?
    
    var avatar_large : String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    override var description: String {
        // 当计算性属性只有get方法时,可以省略
        //        get {
        //        }
        let key = ["access_token", "expires_in", "expiresDate", "uid","screen_name","avatar_large"]
        
        return "UserAccount模型: \(dictionaryWithValuesForKeys(key).description)"
    }

}
